package com.pbq.pickerlib.wheel;

import android.content.Context;


import com.pbq.pickerlib.wheel.adapters.WheelTextAdapter;

import java.util.Date;
import java.util.List;

/**
 * Created by remilia on 2016/10/12.
 */
public class DateWheelManagerBuilder {

    private static DateWheelManagerBuilder builder;
    private WheelTextAdapter mYear;
    private WheelTextAdapter mMonth;
    private WheelTextAdapter mDay;

    private DateWheelManagerBuilder() {
    }

    public static DateWheelManagerBuilder getInstance() {
        if (builder == null) {
            builder = new DateWheelManagerBuilder();
        }
        return builder;
    }

    public TextWheelManager build(Context context) {
        mYear = new WheelTextAdapter(context);
        mDay = new WheelTextAdapter(context);
        mMonth = new WheelTextAdapter(context);
        mYear.setList(getYear());
        mMonth.setList(month);
        mDay.setList(getDay(1900, 1));
        TextWheelManager manager = new TextWheelManager(context, mYear, mMonth, mDay);
        manager.setOnScrollSelectListener(mOnScrollSelectListener);
        return manager;
    }

    private String[] getYear() {
        Date date = new Date(System.currentTimeMillis());
        int year = date.getYear();
        String[] strs = new String[year];
        for (int i = 0; i < year; i++) {
            strs[i] = String.valueOf(year + 1900 - i);
        }
        return strs;
    }

    private static String[] month = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};

    private String[] getDay(int month, int year) {
        int day;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                day = 30;
                break;
            case 2:
                if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
                    day = 29;
                } else {
                    day = 28;
                }
                break;
            default:
                day = 30;
        }
        String[] strs = new String[day];
        for (int i = 1; i <= day; i++) {
            strs[i - 1] = String.valueOf(i);
        }
        return strs;
    }

    private TextWheelManager.OnScrollSelectListener mOnScrollSelectListener = new TextWheelManager.OnScrollSelectListener() {
        @Override
        public void onScrollSelect(Context context, List<WheelView> wheelViews, int positionP, List<WheelTextAdapter> adapters) {
            if (positionP == 1) {
                int year = 1900;
                int month = 1;
                try {
                    year = Integer.parseInt(String.valueOf(adapters.get(0).getItemText(wheelViews.get(0).getCurrentItem())));
                    month = Integer.parseInt(String.valueOf(adapters.get(1).getItemText(wheelViews.get(1).getCurrentItem())));
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
                mDay.setList(getDay(month, year));
                wheelViews.get(2).setViewAdapter(mDay);
                wheelViews.get(2).setCurrentItem(0);
            }
        }
    };
}
